﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenSSL.X509;
using Sicurezza.Entita;
using Sicurezza.Manager;
using System.IO;
using System.Configuration;

namespace Sicurezza.Client.Manager
{
    public class MailManager
    {

        private static string SmtpHost = ConfigurationManager.AppSettings["SmtpHost"];

        public static object GetRecipients(X509Certificate cert, string password, bool Bsignature, bool Bencipher)
        {
            string signature = Convert.ToString(Bsignature);
            string encipher = Convert.ToString(Bencipher);

           byte[] sign= SSLManager.Sign(cert.SerialNumber.ToString()+signature+encipher ,
                            cert,
                            password);

           string list = WSManager.GetRecipients(cert.SerialNumber.ToString(), signature, encipher, sign);

           return XmlManager.DeserializeList(list);


        }

        public static void Send(SignedMessage sm, 
                                 string from,
                                 string to, 
                                 string SmtpUsername,
                                 string SmtpPassword)
        {
            string serializedMessage = XmlManager.Serialize(sm);
            if (File.Exists("Message.xml"))
                File.Delete("Message.xml");
            File.WriteAllText("Message.xml", serializedMessage);

            

            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(from, to);
            Message.Subject = "Files from " + from;
            Message.Attachments.Add(new System.Net.Mail.Attachment("Message.xml"));

            System.Net.NetworkCredential basicCredential = 
                new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
            System.Net.Mail.SmtpClient smtpc = new System.Net.Mail.SmtpClient(SmtpHost);
            smtpc.UseDefaultCredentials = false;
            smtpc.Credentials = basicCredential;
            smtpc.EnableSsl = true;

            try
            {
                smtpc.Send(Message);
            }
            catch (Exception ex)
            {
                throw new ManagerException("Unable to send mail message. Server unreachable?");
            }
        }

        public static MailMessage Get(string message)
        {
            MailMessage mm = null;
            try
            {
                mm = XmlManager.DeserializeMailMessage(message);
            }
            catch (Exception ex)
            {
                throw new Exception("Cannot load file. File is not in correct format.");
            }

            return mm;
        }


        public static List<string> GetFiles(MailMessage mm, X509Certificate recipient, string password)
        {
            List<string> filepaths = new List<string>();
            List<byte[]> files = SSLManager.Decipher(mm, recipient, password);

            for (int i = 0; i < files.Count; i++)
            {
                filepaths.Add(FileManager.SaveTempFile(files[i], mm.Files[i]));
            }

            return filepaths;
        }
    }
}
